<template>
	<view class="box">
		<cu-custom :isBack="true" :publicBackBj="true" fhColor="#fdfdfd" textColor="#000">
			<!-- <block slot="content">商品详情</block> -->
		</cu-custom>
		<view class="ShopDetailsBox">
			<scroll-view scroll-y="true" class="scroll" :style="{height:heighta+'px'}">
				<swiper :autoplay="true" :interval="3000" :duration="1000" class="Swiper">
					<swiper-item v-for="(item, index) in GoodsDetails.goodsDetailImageList" :key="index"
						class="SwiperItem">
						<image :src="item" mode="heightFix" class="SwiperItemImage"></image>
					</swiper-item>
				</swiper>
				<view class="bgc"></view>
				<view class="GoodsInformation">
					<view class="GoodsName">
						{{ GoodsDetails.goodsName }}
					</view>
					<view class="GoodsPrice">
						<view class="RMB"> ￥ </view>
						<view class="Price" v-if="GoodsDetails.goodsPrice">
							{{ GoodsDetails.goodsPrice }}
						</view>
						<view class="Price" v-if="!GoodsDetails.goodsPrice">
							暂无售价
						</view>
					</view>
				</view>

				<view class="GoodsDetails" v-if="GoodsDetails.remark">
					<view class="Title">
						商品详情
					</view>
					<view class="Content">
						<u-parse :content="GoodsDetails.remark" @navigate="navigate"
							v-if="GoodsDetails.remark"></u-parse>
					</view>
				</view>
				<view class="GoodsDetails" v-else>
					<view class="Title">
						暂无数据
					</view>
				</view>
				<view class="Block" v-if="userInfo.type&&userInfo.type===20">

				</view>
			</scroll-view>
			<view class="AddToShopCartBtn" v-if="userInfo.type&&userInfo.type===20">
				<view class="Lh">
					<view class="collect">
						<image :src="imgUrl+'classification/collect.png'" mode=""></image>
						<text>收藏</text>
					</view>
					<view class="collect">
						<image :src="imgUrl+ 'classification/service.png'" mode=""></image>
						<text>客服</text>
					</view>
				</view>
				<view class="Btn" @click="AddToShopCart"> 加入购物车 </view>
			</view>
		</view>
		<!-- 购物车弹窗 -->
		<view class="shopCard" v-if="showCard" @click="showCard=false">
			<view class="card" @click.stop="">
				<view class="top">
					<image class="preview" :src="GoodsDetails.goodsSpecificationVos[pitch].previewImg" mode=""></image>
					<view class="particulars">
						<view class="title">{{GoodsDetails.goodsName}}</view>
						<view class="price" v-if="GoodsDetails.goodsSpecificationVos[pitch].goodsPrice"> <text>￥</text>
							{{GoodsDetails.goodsSpecificationVos[pitch].goodsPrice}}
						</view>
						<view class="price" v-if="!GoodsDetails.goodsSpecificationVos[pitch].goodsPrice"> <text>￥</text>
							暂无售价
						</view>
						<view class="inventory">库存：{{GoodsDetails.goodsSpecificationVos[pitch].stock}}个</view>
						<view class="selected">
							<!-- <view class="left">请选择规格</view> -->
							<view class="right">已选择：{{GoodsDetails.goodsSpecificationVos[pitch].specificationName}}
							</view>
						</view>
					</view>
				</view>
				<view class="specification">
					<view class="subhead">规格</view>
					<scroll-view scroll-y="true" class="option">
						<view class="list" :class="index===pitch?'active':''"
							v-for="(i,index) in GoodsDetails.goodsSpecificationVos" @click="liskClick(index)"
							:key="index">
							<image class="pictogram" :src="i.previewImg" mode=""></image>
							<view class="information">
								{{i.specificationName}}
							</view>
						</view>
					</scroll-view>
				</view>
				<view class="bottom">
					<div class="cardNum">
						<view class="text">购买数量</view>
						<view class="right">
							<view class="addList jian" @click="changeNum('jian')">
								<text class="cuIcon-move"></text>
							</view>
							<input class="text" type="number" v-model.trim="goodsNum" @blur="goodsNumChange(goodsNum)">
							<view class="addList jia">
								<text class="cuIcon-add" @click="changeNum('jia')"></text>
							</view>
						</view>
					</div>
					<view class="btn" @click="addGoodCard">
						加入购物车
					</view>
				</view>
				<uni-icons class="close" type="closeempty" color="#333" @click="showCard=false"></uni-icons>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		Base64
	} from '../../static/lib/base64.js'
	import uParse from "../../components/feng-parse/parse.vue"
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				goodsCode: "",

				GoodsDetails: {},
				heighta: '',
				imgUrl: this.$http.imgUrl + '/static/',
				showCard: false,
				// 选中
				pitch: 0,
				goodsNum: 1,
				// userType:''
			};
		},
		components: {
			uParse
		},
		computed: {
			...mapState('user', ['userInfo'])
		},
		onLoad({
			id
		}) {
			// console.log(option, "options");
			this.goodsCode = id;
			this.getData();
		},
		mounted() {
			this.heighta = uni.getSystemInfoSync().windowHeight
			// console.log(this.userInfo, 'userInfo');
			// this.userType=this.userInfo.data.data.type
		},
		methods: {
			async getData() {
				const url = "/app/goods/goods_detail";
				const res = await this.$http.get(url, {
					goodsCode: this.goodsCode,
					token: uni.getStorageSync('token')
				});
				this.GoodsDetails = res.data.data;
				this.GoodsDetails.remark = Base64.decode(this.GoodsDetails.remark)
				this.GoodsDetails.remark = this.GoodsDetails.remark.replace(/\"/g, "'")
				// this.GoodsDetails.remark = 1
				// console.log(res, "商品详情");
			},
			async getPlaceAnOrderStatus() {
				const res = await this.$http.get("/app/book_order/getBookStatus");
				// console.log(res, '订货状态');
				return res.data.data;
			},

			// 切换选项
			liskClick(i) {
				let goodItem = this.GoodsDetails.goodsSpecificationVos[i]
				if (goodItem.stock <= 0) {
					uni.showToast({
						title: '暂无库存',
						icon: 'none'
					})
					return
				}
				this.pitch = i
				this.goodsNum = 1
			},

			// 打卡购物车弹窗
			AddToShopCart() {
				if (this.GoodsDetails.goodsSpecificationVos.length === 0) {
					uni.showToast({
						title: '暂无库存',
						icon: 'none'
					})
					return
				} else {
					this.showCard = true
				}
			},
			// 点击加减购物车数量
			changeNum(type) {
				let goodItem = this.GoodsDetails.goodsSpecificationVos[this.pitch]
				if (type === 'jian') {
					if (this.goodsNum <= 1) {
						this.goodsNum = 1
						uni.showToast({
							title: '最少购买一件',
							icon: 'none'
						})
					} else {
						this.goodsNum--
					}
				} else if (type === 'jia') {
					if (this.goodsNum >= goodItem.stock) {
						this.goodsNum = goodItem.stock
						uni.showToast({
							title: `最多购买${goodItem.stock}件`,
							icon: "none"
						})
					} else {
						this.goodsNum++
					}
				}
			},
			// 输入数量
			goodsNumChange(item) {
				let goodItem = this.GoodsDetails.goodsSpecificationVos[this.pitch]
				if (item > goodItem.stock) {
					this.goodsNum = goodItem.stock
					uni.showToast({
						title: `最多购买${goodItem.stock}件`,
						icon: "none"
					})
				} else if (item < 1) {
					this.goodsNum = 1
					uni.showToast({
						title: '最少购买一件',
						icon: 'none'
					})
				} else {
					this.goodsNum = item
				}
			},
			// 加入购物车
			async addGoodCard() {
				let goodItem = this.GoodsDetails.goodsSpecificationVos[this.pitch]
				if (goodItem.stock !== 0) {
					const url = this.api.shopCart.AddToShopCart;
					const res = await this.$http.post(url, {
						// goodCode: this.GoodsDetails.goodsCode,
						goodsCode: this.goodsCode,
						goodsNum: this.goodsNum,
						specificationId: goodItem.id
					});
					if (res.data.code === 200) {
						uni.showToast({
							title: "加入成功",
						});
						// uni.navigateTo({
						// 	url: '/pagesA/My/shoppingCard'
						// })
					}
					if (res.data.code === 500) {
						uni.showToast({
							title: '商品库存不足',
							icon: 'error'
						})
					}
				} else {
					uni.showToast({
						title: '库存不足',
						icon: 'error'
					})
				}
			}
		},
	};
</script>

<style lang="scss" scoped>
	.box {
		.scroll {
			position: fixed;
			top: 0;
			left: 0;
		}

		.Swiper {
			z-index: -1;
			width: 100%;
			height: 750rpx;

			.SwiperItem {
				width: 750rpx;
				height: 750rpx;

				.SwiperItemImage {
					width: 750rpx !important;
					height: 750rpx !important;
				}
			}
		}

		.bgc {
			position: absolute;
			left: 0;
			top: 702rpx;
			width: 100%;
			height: 48rpx;
			background: linear-gradient(180deg, rgba(255, 255, 255, 0) 0%, #FAFAFA 100%);
		}

		.GoodsInformation {
			width: 702rpx;
			height: 208rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 10rpx 20rpx 2rpx rgba(0, 0, 0, 0.05);
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			opacity: 1;
			margin-left: 20rpx;
			margin-top: -48rpx;
			padding: 24rpx 30rpx 0 32rpx;
			position: absolute;
			z-index: 1;

			.GoodsName {
				font-size: 32rpx;
				font-weight: 600;
				color: #333333;
				height: 96rpx;
				line-height: 48rpx;

				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-line-clamp: 2;
				-webkit-box-orient: vertical;

				view {
					font-size: 25rpx;
					color: gray;
					margin-left: 10rpx;
				}
			}

			.GoodsPrice {
				display: flex;
				align-items: center;
				color: #E34949;
				margin-top: 16rpx;

				.RMB {
					font-size: 22rpx;
					margin-right: 12rpx;
				}

				.Price {
					font-size: 32rpx;
				}
			}
		}

		.GoodsDetails {
			width: 100%;
			margin-top: 232rpx;
			border-radius: 16rpx;

			.Title {
				font-size: 24rpx;
				font-weight: 400;
				color: #666666;
				display: flex;
				align-items: center;
				justify-content: center;

			}

			.Title::before {
				content: '';
				display: inline-block;
				width: 120rpx;
				height: 2rpx;
				background-color: rgba(51, 51, 51, 0.2);
				;
				margin-right: 16rpx;
			}

			.Title::after {
				content: '';
				display: inline-block;
				width: 120rpx;
				height: 2rpx;
				background-color: rgba(51, 51, 51, 0.2);
				;
				margin-left: 16rpx;
			}

			.Content {
				margin-top: 32rpx;
				border-radius: 24rpx 24rpx 0 0;
				padding-bottom: 50rpx;
				// background-color: orange;
				// padding: 150rpx 50rpx 150rpx 50rpx;
			}
		}

		.Block {
			width: 100%;
			height: 124rpx;
		}

		.AddToShopCartBtn {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			height: 146rpx;
			background-color: #FFFFFF;
			box-shadow: 0rpx -2rpx 12rpx 2rpx rgba(138, 138, 138, 0.16);
			display: flex;
			padding: 0 24rpx;
			justify-content: space-between;
			// align-items: center;
			padding-top: 16rpx;

			.Lh {
				flex: 1;
				display: flex;
				justify-content: space-between;

				.collect {
					display: flex;
					flex-direction: column;
					align-items: center;

					image {
						width: 40rpx;
						height: 40rpx;
					}

					text {
						font-size: 20rpx;
						font-weight: 400;
						color: #666666;
					}
				}
			}

			.Btn {
				width: 526rpx;
				height: 76rpx;
				background: linear-gradient(135deg, #4D8BFF 0%, #78D8FB 100%);
				border-radius: 38rpx;
				text-align: center;
				line-height: 76rpx;
				font-size: 28rpx;
				font-weight: 500;
				color: #FFFFFF;
				margin-left: 32rpx;
			}
		}

		// 加入购物车
		.shopCard {
			position: fixed;
			top: 0;
			left: 0;
			width: 100%;
			height: 100vh;
			background: rgba(0, 0, 0, 0.5);

			.card {
				position: fixed;
				left: 0;
				bottom: 0;
				width: 750rpx;
				// height: 812rpx;
				background: #FFFFFF;
				border-radius: 24rpx 24rpx 0rpx 0rpx;
				padding: 0 32rpx;

				// 上部分产品信息
				.top {
					padding: 32rpx 0;
					display: flex;
					height: 264rpx;
					width: 100%;
					border-bottom: 1rpx solid rgba(51, 51, 51, 0.08);

					.preview {
						width: 200rpx;
						height: 200rpx;
					}

					.particulars {
						margin-left: 16rpx;

						.title {
							margin-top: 16rpx;
							font-size: 32rpx;
							font-weight: 600;
							color: #333333;
						}

						.price {
							margin-top: 16rpx;
							font-size: 36rpx;
							font-weight: 600;
							color: #FF5C46;

							text {
								font-size: 16rpx;
							}
						}

						.inventory {
							margin-top: 16rpx;
							font-size: 24rpx;
							font-weight: 400;
							color: #333333;
						}

						.selected {
							margin-top: 16rpx;
							display: flex;
							font-size: 24rpx;
							font-weight: 400;

							.left {
								color: #999999;
								margin-right: 24rpx;
							}

							.right {
								color: #333333;
							}
						}
					}
				}

				// 中部选项
				.specification {
					padding: 32rpx 0;
					width: 100%;

					.subhead {
						font-size: 28rpx;
						font-weight: 500;
						color: #333333;
						// margin: 32rpx 0 24rpx;
						margin-bottom: 24rpx;
					}

					.option {
						max-height: 704rpx;
						width: 100%;
						display: flex;
						flex-wrap: wrap;

						.list {
							// width: 228rpx;
							display: inline-flex;
							align-items: center;
							height: 72rpx;
							background: rgba(153, 153, 153, 0.12);
							border-radius: 8rpx;
							margin-bottom: 16rpx;
							margin-right: 16rpx;
							box-sizing: border-box;

							.pictogram {
								margin-left: 8rpx;
								width: 56rpx;
								height: 56rpx;
							}

							.information {
								font-size: 24rpx;
								font-weight: 400;
								color: #333333;
								margin: 0 16rpx 0 12rpx;
							}

							&.active {
								background: rgba(77, 139, 255, 0.2);
								border: 2rpx solid #4D8BFF;

								.information {
									color: #4D8BFF;
								}
							}
						}
					}
				}

				// 底部按钮
				.bottom {
					border-top: 1rpx solid rgba(51, 51, 51, 0.08);
					height: 268rpx;
					width: 100%;
					padding-top: 32rpx;

					.cardNum {
						display: flex;
						height: 48rpx;
						width: 100%;
						justify-content: space-between;
						align-items: center;
						margin-bottom: 38rpx;

						.text {
							font-size: 28rpx;
							font-weight: 500;
							color: #333333;
						}

						.right {
							display: flex;
							align-items: center;
							border: 1rpx solid #D7D7D7;
							border-radius: 10rpx;
							height: 48rpx;

							.addList {
								width: 44rpx;
								height: 100%;
								display: flex;
								align-items: center;
								justify-content: center;

								text {
									font-size: 30rpx;
								}
							}

							.jian {
								border-right: 1rpx solid #D7D7D7;

								text {
									font-size: 22rpx;
								}

							}

							.jia {
								border-left: 1rpx solid #D7D7D7;
							}

							.text {
								width: 84rpx;
								text-align: center;
								font-size: 24rpx;
								font-weight: 500;
								color: #333333;
							}
						}
					}

					.btn {
						width: 702rpx;
						height: 76rpx;
						background: linear-gradient(135deg, #78D8FB 0%, #4D8BFF 100%);
						border-radius: 38rpx;
						font-size: 28rpx;
						font-weight: 600;
						color: #FFFFFF;
						text-align: center;
						line-height: 76rpx;
					}
				}

				.close {
					width: 28rpx;
					height: 28rpx;
					position: absolute;
					top: 32rpx;
					right: 32rpx;
					font-size: 25rpx;
					font-weight: 700;
				}
			}
		}

	}
</style>